import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter); // 使用插件

import Home from "@/pages/Home";
import Login from "@/pages/Login";
import Register from "@/pages/Register";
import Search from "@/pages/Search";

let originPush = VueRouter.prototype.push; // 保留原始方法（备份）
let originReplace = VueRouter.prototype.replace; // 备份replace
// 重写push方法
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        originPush.call(this, location, resolve, reject);
    } else {
        originPush.call(this, location, () => {}, () => {});
    }
};

// 重写replace方法
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        originReplace.call(this, location, resolve, reject);
    } else {
        originReplace.call(this, location, () => {}, () => {});
    }
};

export default new VueRouter({
    routes: [
        {
            path: "/home",
            component: Home,
            meta: {
                isShowFooter: true
            }
        },
        {
            name: "search",
            path: "/search/:keyword?",
            component: Search,
            meta: {
                isShowFooter: true
            },
            props: function ($route) {
                console.log($route);
                return {
                    keyword: $route.params.keyword
                }
            }
        },
        {
            path: "/login",
            component: Login
        },
        {
            path: "/register",
            component: Register
        },
        {
            path: "*",
            redirect: "/home" // 重定向写法
        }
    ]
})